package edu.calpoly.android.echoes;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.graphics.Paint;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Echoes extends Activity {
	
	//Objects required to play multiple simple sounds
	private SoundPool m_soundPool;
	private HashMap<Integer, Integer> m_soundPoolMap;
	private AudioManager m_audioManager;
	
	private ArrayList<String> crash = new ArrayList<String>();
	
	//Future class to control sound
	private AudioPlayer m_audioPlayer;
	
	//Current set number of possible simultaneous music streams
	private static final int MAX_SOUND_STREAMS = 4;
	//Max frequency of the SoundPool class
	private static final float MAX_PLAYBACK_RATE = 2.0f;
	//Min frequency of the SoundPool class
	private static final float MIN_PLAYBACK_RATE = 0.5f;
	//ID of the test sound
	private static final int PLAY_TEST_SOUND = 1;
	
	public static int MAIN_SCREEN = 0, GAME_SCREEN = 1;
	public static int CURRENT_SCREEN = 0;
	public static boolean DISPLAY_PAUSE_SCREEN = false;
	
	private Context m_context;
	
	ShapeDBAdapter m_shapeDB;
	DrawView _dv;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        m_context = getBaseContext();
        m_audioPlayer = new AudioPlayer(m_context);
        
        initButtons();
        
        m_shapeDB = new ShapeDBAdapter(this);
        m_shapeDB.open();
        
        //m_shapeDB.newGame();
        
        Paint p = new Paint();
        p.setColor(1);
        MyCircle circle = new MyCircle(50, 50, 20, p, m_audioPlayer);
        MyRectangle rect = new MyRectangle(50, 50, 100, 100, p, 1);
        
        /*long circID = m_shapeDB.insertShape(circle);
        long lineID = m_shapeDB.insertShape(line);
        long rectID = m_shapeDB.insertShape(rect);*/
        
        //ArrayList<MyCircle> circs = m_shapeDB.getAllCircles();
        //Log.e("OnCreate", circs.get(0).toString() + " " + circs.size());
        //Log.e("OnCreate", " " + circs.size());
        //Log.e("Circles", circs.get(0).getX() + " " + circs.get(0).getY() + " " + circs.get(0).getR());
        
      //  ArrayList<MyLine> lines = m_shapeDB.getAllLines();
        //Log.e("OnCreate", lines.get(0).toString() + " " + lines.size());
        //Log.e("OnCreate", " " + lines.size());
        
        //ArrayList<MyRectangle> rects = m_shapeDB.getAllRectangles();
        //Log.e("OnCreate", rects.get(0).toString() + " " + rects.size());
        //Log.e("OnCreate", " " + rects.size());
        
       // MyDrawableShapes mds = m_shapeDB.getShape(5);
       // Log.e("MyDrawableShape", mds.toString());
       // MyDrawableShapes mds2 = m_shapeDB.getShape(6);
       // MyDrawableShapes mds3 = m_shapeDB.getShape(7);
        
        //boolean result = m_shapeDB.removeShape(2);
       /* for(int i = 0; i < 90; i++) {
        	boolean result = m_shapeDB.removeShape(i);
        	if(!result)
            	Log.e("OnCreate", "error");
        }*/
        CURRENT_SCREEN = MAIN_SCREEN;
        Display display = getWindowManager().getDefaultDisplay();
        _dv = null;
        //TODO: remove tests
        //addSound(PLAY_TEST_SOUND, R.raw.bell);
    }
    
    /** 
     * Initializes buttons on the main screen and their listeners
     */
    public void initButtons() {
    	Button newGame = (Button)findViewById(R.id.m_newGame);
        newGame.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		//Start a new game
        		long gameID = m_shapeDB.newGame();
        		//m_shapeDB.insertShape(new MyCircle(1.0f,10.0f,1.0f, new Paint(), m_audioPlayer));
        		//m_shapeDB.insertShape(new MyRectangle(1.0f, 1.0f, 1.0f, 1.0f, new Paint()));
        		_dv = new DrawView(m_context, m_audioPlayer, m_shapeDB, true);
        		setContentView(_dv);
        		CURRENT_SCREEN = GAME_SCREEN;
        	}
        });
        
        /*Following buttons initialized for future use */
  /*      Button resGame = (Button)findViewById(R.id.m_resGame);
        resGame.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		int gameNum = -1;
        		Cursor c = m_shapeDB.getSavesCursor();
        		c.moveToFirst();
        		while(!c.isAfterLast()) {
        			gameNum = c.getInt(ShapeDBAdapter.GAME_COL_ID);
        		}
        		m_shapeDB.loadGame(gameNum);
        		setContentView(new DrawView(m_context, m_audioPlayer, m_shapeDB, true));
        	}
        });*/


        Button loadGame = (Button)findViewById(R.id.m_loadGame);
        loadGame.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		Intent myIntent = new Intent(getBaseContext(), saveList.class);
        		startActivity(myIntent);
        	}
        });
        
        Button tutorial = (Button)findViewById(R.id.m_tutorial);
        tutorial.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		Intent myIntent = new Intent(getBaseContext(), TutorialView.class);
        		startActivity(myIntent);
        	}
        });
        
        /*
        Button settings = (Button)findViewById(R.id.m_settings);
        settings.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		
        	}
        });*/
        
        Button credits = (Button)findViewById(R.id.m_credits);
        credits.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		Intent myIntent = new Intent(getBaseContext(), CreditView.class);
        		startActivity(myIntent);
        		//setContentView(R.layout.credit_view);
        	}
        });
        
        Button exit = (Button)findViewById(R.id.m_exit);
        exit.setOnClickListener(new OnClickListener() {
        	public void onClick(View arg0) {
        		finish();
        	}
        });
    }
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_MENU) {
    		if(CURRENT_SCREEN == MAIN_SCREEN) {
    			return false;
    		} else if(CURRENT_SCREEN == GAME_SCREEN) {
    		    DISPLAY_PAUSE_SCREEN = !DISPLAY_PAUSE_SCREEN;
    			return true;
    		}
    	} else if (keyCode == KeyEvent.KEYCODE_BACK) {
    		if(CURRENT_SCREEN == GAME_SCREEN) {
    		}
    	}
    	return false;
    }



    /*public boolean onCreateOptionsMenu(Menu menu) {
    	if(CURRENT_SCREEN == GAME_SCREEN) {
	    	super.onCreateOptionsMenu(menu);
	    	
	    	int groupId = 0;
	    	int menuItemId = Menu.FIRST;
	    	int menuItemOrder = Menu.NONE;
	    	int menuItemText = R.string.menu_item;
	    	
	    	MenuItem menuItem = menu.add(groupId, menuItemId, menuItemOrder, menuItemText);
	    	DISPLAY_PAUSE_SCREEN = true;
	    	return true;
    	}
    	else if(CURRENT_SCREEN == MAIN_SCREEN) {
    		return false;
    	}
    	else {
    		return false;
    	}
    }
    
    public void onOptionsMenuClosed(Menu menu) {
    	super.onOptionsMenuClosed(menu);
    	
    	DISPLAY_PAUSE_SCREEN = false;
    }*/

}